package cz.acies.genea.ui.chart;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.google.gson.JsonObject;

import cz.acies.genea.data.persist.GedPerson;
import cz.acies.genea.ui.svg.G2Color;
import cz.acies.genea.ui.svg.G2Rectangle;
import cz.acies.genea.ui.svg.G2Text;

public class TreeChartCard 
{
	public static double DEFAULT_WIDTH = 160;
	public static double DEFAULT_HEIGHT = 80;
	
	public double ox = 0;
	public double oy = 0;

	public double x = 0;
	public double y = 0;
	public double width = DEFAULT_WIDTH;
	public double height = DEFAULT_HEIGHT;

	public double margin = 0;
	
	protected boolean selected = false;
	
	private G2Rectangle rect;
	private G2Text caption;

	private GedPerson person;
	
	public TreeChartCard() {}

	public TreeChartCard(GedPerson person) {
		this.person = person;
	}

	public void compute() {}
	
	public List<Object> rendered() throws Exception {
		compute();
		List<Object> gdata = new ArrayList<Object>();
		
		if (this.person == null || !person.hasId()) {
			rect = new G2Rectangle(ox + x, oy + y, width, height);
			rect.getDrawAttr().setForeground(new G2Color(192, 192, 192));
			JsonObject element = new JsonObject();
			JsonObject jsobj = rect.toJSON(element);
			gdata.add(jsobj.get("draw"));
			return gdata; 
		}
		String header = ""+person.getDisplayName();
		rect =  new G2Rectangle(ox + x, oy + y, width, height);
		if (person.isFamale()) {
			rect.getDrawAttr().setBackground(new G2Color(253, 194, 223));
		} else {
			rect.getDrawAttr().setBackground(new G2Color(194, 223, 253));
		}
		caption = new G2Text(ox + x + 30, oy + y + 15, header);
		caption.setFontSise(12);
		
		JsonObject element = new JsonObject();
		JsonObject jsobj = rect.toJSON(element);
		gdata.add(jsobj.get("draw"));
		
		element = new JsonObject();
		jsobj = caption.toJSON(element);
		gdata.add(jsobj.get("draw"));
		
		int pos = 32;
		if (person.isFamale() && person.hasMaidenName()) {
			String name = "( roz." + person.getMaidenName() + " )";
			G2Text text = new G2Text(ox + x + 30, oy + y + pos, name);
			text.setFontSise(11);
			element = new JsonObject();
			jsobj = text.toJSON(element);
			gdata.add(jsobj.get("draw"));
			pos += 15;
		}
		int aktYear = cz.acies.utils.DateUtils.getYear(new Timestamp(System.currentTimeMillis()));
		
		if (person.hasBirthDate()) {
			String date = "* " + DateFormatUtils.format(person.getBirthDate(), "dd.M.yyyy");
			G2Text text = new G2Text(ox + x + 30, oy + y + pos, date);
			text.setFontSise(11);
			element = new JsonObject();
			jsobj = text.toJSON(element);
			gdata.add(jsobj.get("draw"));
			pos += 15;
		}
		if (person.hasDeathDate()) {
			String date =  "+ " + DateFormatUtils.format(person.getDeathDate(), "dd.M.yyyy");
			G2Text text = new G2Text(ox + x + 30, oy + y + pos, date);
			text.setFontSise(11);
			element = new JsonObject();
			jsobj = text.toJSON(element);
			gdata.add(jsobj.get("draw"));
		}
		return gdata;
	}

	public GedPerson getPerson() {
		return person;
	}
	
	public boolean hasPerson() {
		return (this.person != null && this.person.hasId());
	}
	
	public void setPerson(GedPerson person) {
		this.person = person;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	// -----  -----------------------------------------------------------------
	
	public double getLeft() {
		return ox + x;
	}

	public double getTop() {
		return oy + y;
	}

	public double getRight() {
		return ox + x + width;
	}
	
	public double getBottom() {
		return oy + y + height;
	}

	public double getCenterX() {
		return ox + x + (width/2);
	}
	
	public double getCenterY() {
		return oy + y + (height/2);
	}

}
